package com.bike.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.util.StringUtils;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

public class PageData extends HashMap implements Map{    
	
	public int getInteger(String key) {
		Object value = get(key);
		if(StringUtils.isEmpty(value)){
			return 0;
		}
		return Integer.parseInt( value.toString());
	}
	public Long getLong(String key) {
		Object value = get(key);
		if(StringUtils.isEmpty(value)){
			return 0L;
		}
		return Long.parseLong( value.toString().replaceAll("'", ""));
	}

	@Override
	public Object get(Object key) {
		Object value = super.get(key);
		if(key.equals("sort")){
			if(value!=null){
				String order=value.toString();
				System.out.println(TransFerUtil.getMybatisField(order));
				return TransFerUtil.getMybatisField(order);
			}
		}
		
		return value;
	}
	
	public String getString(String key) {
		Object value = get(key);
		if(value==null){
			return "";
		}
		return  value.toString();
	}
	public Double getDouble(String key) {
		Object value = get(key);
		if(value==null){
			return 0d;
		}
		return Double.parseDouble( value.toString());
	}
	
	public static PageData getToPageData(HttpServletRequest request){
    	Map map = null;
        Map properties = request.getParameterMap();
        PageData returnMap = new PageData(); 
        Iterator entries = properties.entrySet().iterator(); 
        Map.Entry entry; 
        String name = "";  
        String value = "";  
        while (entries.hasNext()) {
            entry = (Map.Entry) entries.next(); 
            name = (String) entry.getKey(); 
            Object valueObj = entry.getValue(); 
            if(null == valueObj){ 
                value = ""; 
//此处需要注意的是前台传过来的是String数组对象就用String[]来判断，如果传递过来是json对象可以用JSONObject来尝试(这种方法没试过，应该是可以的)，
//也可以传递在前台拼接个字符串后，后台直接用String来判断，然后用split方法来截断(这种当然可以，没试过，也不太方便)
            }else if(valueObj instanceof String[]){ 
                String[] values = (String[])valueObj;
                for(int i=0;i<values.length;i++){ 
                     value = values[i] + ",";
                }
                value = value.substring(0, value.length()-1); 
            }else{
                value = valueObj.toString(); 
            }
            returnMap.put(name, value); 
        }
       return returnMap;
    }
    
    public static Map<String, Object> getPageData(HttpServletRequest request){
    	Map map = null;
        Map properties = request.getParameterMap();
        Map<String, Object> returnMap = new HashMap<String, Object>(); 
        Iterator entries = properties.entrySet().iterator(); 
        Map.Entry entry; 
        String name = "";  
        String value = "";  
        while (entries.hasNext()) {
            entry = (Map.Entry) entries.next(); 
            name = (String) entry.getKey(); 
            Object valueObj = entry.getValue(); 
            if(null == valueObj){ 
                value = ""; 
//此处需要注意的是前台传过来的是String数组对象就用String[]来判断，如果传递过来是json对象可以用JSONObject来尝试(这种方法没试过，应该是可以的)，
//也可以传递在前台拼接个字符串后，后台直接用String来判断，然后用split方法来截断(这种当然可以，没试过，也不太方便)
            }else if(valueObj instanceof String[]){ 
                String[] values = (String[])valueObj;
                for(int i=0;i<values.length;i++){ 
                     value = values[i] + ",";
                }
                value = value.substring(0, value.length()-1); 
            }else{
                value = valueObj.toString(); 
            }
            returnMap.put(name, value); 
        }
       return returnMap;
    }
	public static Map<String, Object> getPageData(HttpServletRequest request, int page) {
		if(page<=0){
			page=1;
		}
		Map<String, Object> result=getPageData(request);
		
		PageBounds pageBounds=new PageBounds(page, 20);
		  
		result.put("pageB", pageBounds);
		return result;
	}
	public static PageData getPageDataByPage(HttpServletRequest request) {
		 
		PageData result=getToPageData(request);
		
		Integer page=result.getInteger("page");
		Integer rows=result.getInteger("rows");
		if(page!=null&&page>0){
			if(rows==0){
				rows=20;
			}
			PageBounds pageBounds=new PageBounds(page, rows);
			result.put("pageB", pageBounds);
		}
		return result;
	}
	 
}